package com.tangkun.environment.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.tangkun.environment.R;
import com.tangkun.environment.adapter.JuBaoYiChangDetailAdapter;
import com.tangkun.environment.adapter.RollviewpagerAdapter;
import com.tangkun.environment.entity.base.CommonReturn;
import com.tangkun.environment.entity.gongzuo.ReportViewReturn;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.utils.Utils;
import com.tangkun.environment.view.CommonQueDingDialog;
import com.tangkun.environment.view.CustomListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 举报异常详情
 */
public class JuBaoYiChangDetailActivity extends BaseActivity implements JuBaoYiChangDetailAdapter.MyClickListener {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tv_yichangshujutijiao_zhuangtai)
    TextView tvYichangshujutijiaoZhuangtai;
    @BindView(R.id.tv_yichangshujutijiao_name)
    TextView tvYichangshujutijiaoName;
    @BindView(R.id.tv_yichangshujutijiao_shijian)
    TextView tvYichangshujutijiaoShijian;
    @BindView(R.id.tv_yichangshujutijiao_content)
    TextView tvYichangshujutijiaoContent;
    @BindView(R.id.custom_list_view)
    CustomListView customListView;
    @BindView(R.id.rollviewpager)
    RollPagerView rollPagerView;
    private Context mContext;

    /**
     * ID
     */
    private String id = "";
    /**
     * 数据源
     */
    private List<ReportViewReturn.DataBean.FlowlistBean> dataList = new ArrayList<ReportViewReturn.DataBean.FlowlistBean>();
    /**
     * 适配器
     */
    private JuBaoYiChangDetailAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jubaoyichang_detail);
        mContext = this;
        ButterKnife.bind(this);
        id = getIntent().getStringExtra("id");
        title.setText("详情");
        initData();
        reportView();
    }

    private void initData() {
        adapter = new JuBaoYiChangDetailAdapter(mContext, dataList);
        adapter.setMyClickListener(this);
        customListView.setAdapter(adapter);
    }

    /**
     * 异常数据提交 详情
     */
    private void reportView() {
        fromNetwork.reportView(id).compose(new RxActivityHelper<ReportViewReturn>().ioMain(JuBaoYiChangDetailActivity.this, true))
                .subscribe(new MySubscribe<ReportViewReturn>(JuBaoYiChangDetailActivity.this, true) {
                    @Override
                    protected void success(ReportViewReturn resultBean) {
                        if (resultBean.data != null) {
                            /*** 轮播图设置 ***/
                            if (!TextUtils.isEmpty(resultBean.data.reportimgs)) {
                                //轮播间隔时间
                                rollPagerView.setPlayDelay(30000 * 1000);
                                //动画时间
                                rollPagerView.setAnimationDurtion(500);
                                //设置指示器颜色
                                rollPagerView.setHintView(new ColorPointHintView(JuBaoYiChangDetailActivity.this, getResources().getColor(R.color.common_green), getResources().getColor(R.color.common_bg)));
                                //刷新广播轮播图
                                String reportimgs = resultBean.data.reportimgs;
                                String[] reportimgsArray = reportimgs.split(",");
                                List<String> urlList = Arrays.asList(reportimgsArray);
                                RollviewpagerAdapter rollviewpagerAdapter = new RollviewpagerAdapter(JuBaoYiChangDetailActivity.this, urlList);
                                rollPagerView.setAdapter(rollviewpagerAdapter);
                                rollPagerView.setVisibility(View.VISIBLE);
                            } else {
                                rollPagerView.setVisibility(View.INVISIBLE);
                            }

                            //状态 unhandle，待处理；handle，已处理；uncheck、待巡查；check、已巡查；unfile，未归档；file、已归档
                            switch (resultBean.data.statuscode) {
                                case "unhandle"://待处理
                                    tvYichangshujutijiaoZhuangtai.setText("待处理");
                                    tvYichangshujutijiaoZhuangtai.setBackgroundResource(R.drawable.shape_rectangle_red_solid);
                                    break;
                                case "handle"://已处理
                                    tvYichangshujutijiaoZhuangtai.setText("已处理");
                                    tvYichangshujutijiaoZhuangtai.setBackgroundResource(R.drawable.shape_rectangle_green_solid);
                                    break;
                                case "uncheck"://待巡查
                                    tvYichangshujutijiaoZhuangtai.setText("待巡查");
                                    tvYichangshujutijiaoZhuangtai.setBackgroundResource(R.drawable.shape_rectangle_orange_solid);
                                    break;
                                case "check"://已巡查
                                    tvYichangshujutijiaoZhuangtai.setText("已巡查");
                                    tvYichangshujutijiaoZhuangtai.setBackgroundResource(R.drawable.shape_rectangle_green_solid);
                                    break;
                                case "unfile"://未归档
                                    tvYichangshujutijiaoZhuangtai.setText("未归档");
                                    tvYichangshujutijiaoZhuangtai.setBackgroundResource(R.drawable.shape_rectangle_green_solid);
                                    break;
                                case "file"://已归档
                                    tvYichangshujutijiaoZhuangtai.setText("已归档");
                                    tvYichangshujutijiaoZhuangtai.setBackgroundResource(R.drawable.shape_rectangle_green_solid);
                                    break;
                                default://默认情况
                                    tvYichangshujutijiaoZhuangtai.setText(resultBean.data.statuscode + "");
                                    tvYichangshujutijiaoZhuangtai.setBackgroundResource(R.drawable.shape_rectangle_red_solid);
                                    break;
                            }
                            //名称
                            if (!TextUtils.isEmpty(resultBean.data.enterprisename)) {
                                tvYichangshujutijiaoName.setText(resultBean.data.enterprisename);
                            } else {
                                tvYichangshujutijiaoName.setText("");
                            }
                            //内容
                            if (!TextUtils.isEmpty(resultBean.data.content)) {
                                tvYichangshujutijiaoContent.setText(resultBean.data.content);
                            } else {
                                tvYichangshujutijiaoContent.setText("");
                            }
                            //时间
                            if (!TextUtils.isEmpty(resultBean.data.createtime)) {
                                tvYichangshujutijiaoShijian.setText(Utils.getDateYearMonthDay(resultBean.data.createtime));
                            } else {
                                tvYichangshujutijiaoShijian.setText("");
                            }

                            //流程
                            dataList.clear();
                            if (resultBean.data.flowlist != null) {
                                for (int j = 0; j < resultBean.data.flowlist.size(); j++) {
                                    if (j == 0) {//第一步 提交
                                        //标记第一步
                                        resultBean.data.flowlist.get(j).isFirstStep = true;
                                        dataList.add(resultBean.data.flowlist.get(j));
                                    } else if (j == resultBean.data.flowlist.size() - 1) {
                                        //标记最后一步
                                        resultBean.data.flowlist.get(j).isLastStep = true;
                                        dataList.add(resultBean.data.flowlist.get(j));
                                    } else {
                                        //标记非第一步
                                        resultBean.data.flowlist.get(j).isFirstStep = false;
                                        //标记非最后一步
                                        resultBean.data.flowlist.get(j).isLastStep = false;
                                        dataList.add(resultBean.data.flowlist.get(j));
                                    }
                                }
                            }
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage + "");
                    }
                });
    }

    @OnClick({R.id.back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                JumpHelper.finish(mContext);
                break;
        }
    }

    //警示按钮监听事件
    @Override
    public void onStatusClick(int position) {
        String content = dataList.get(position).description;
        if (TextUtils.isEmpty(content)) {
            content = "暂无处理意见";
        }
        final CommonQueDingDialog commonQueDingDialog = new CommonQueDingDialog(JuBaoYiChangDetailActivity.this, "处理意见", content, "确定");
        commonQueDingDialog.show();
        commonQueDingDialog.setOnClickListener(new CommonQueDingDialog.OnClickListener() {
            @Override
            public void rightClick() {
                commonQueDingDialog.dismiss();
            }
        });
    }

    /**
     * 按钮监听事件
     *
     * @param type     0：查看数据 1：编辑数据 2：提交数据 3:审核
     * @param position 索引
     */
    @Override
    public void onBtnClick(int type, final int position) {
        Intent intent;
        switch (type) {
            case 0://查看数据
//                intent = new Intent(JuBaoYiChangDetailActivity.this, BianJiZiLiaoActivity.class);
//                intent.putExtra("id", dataList.get(position).ids);
//                JumpHelper.jump(JuBaoYiChangDetailActivity.this, intent, false);
                break;
            case 1://编辑数据
//                intent = new Intent(JuBaoYiChangDetailActivity.this, BianJiZiLiaoActivity.class);
//                intent.putExtra("id", dataList.get(position).ids);
//                JumpHelper.jump(JuBaoYiChangDetailActivity.this, intent, false);
                break;
            case 2://提交数据
//                intent = new Intent(JuBaoYiChangDetailActivity.this, TiJiaoXunChaShuJuActivity.class);
//                intent.putExtra("id", dataList.get(position).ids);
//                JumpHelper.jump(JuBaoYiChangDetailActivity.this, intent, false);
                break;
            case 3://审核
//                //审核对话框
//                ShenHeDialog shenHeDialog = new ShenHeDialog(JuBaoYiChangDetailActivity.this);
//                shenHeDialog.show();
//                shenHeDialog.setOnClickListener(new ShenHeDialog.OnClickListener() {
//                    @Override
//                    public void access(String content) {
//                        //通过审核
//                        inspectflowAudit(dataList.get(position).ids, 1, content);
//                    }
//
//                    @Override
//                    public void refuse(String content) {
//                        //打回重申
//                        inspectflowAudit(dataList.get(position).ids, 2, content);
//                    }
//                });
                break;
        }
    }

    /**
     * 巡查数据审核
     *
     * @param ids         流程id
     * @param status      是否通过，1 通过；2 未通过
     * @param description 可选 描述
     */
    private void inspectflowAudit(String ids, int status, String description) {
        Map<String, String> map = new HashMap<>();
        map.put("ids", ids);
        map.put("status", status + "");
        map.put("description", description);
        fromNetwork.inspectflowAudit(map)
                .compose(new RxActivityHelper<CommonReturn>().ioMain(JuBaoYiChangDetailActivity.this, true))
                .subscribe(new MySubscribe<CommonReturn>(mContext, true) {
                    @Override
                    protected void success(CommonReturn resultBean) {
                        //刷新列表
                        reportView();
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage);
                    }
                });
    }
}
